package com.example.springbootdemo.text;

import java.util.HashMap;
import java.util.Map;

public class CombinedSimilarity {

    // 假设我们已经有了以下方法的实现
    public static int levenshteinDistance(String s1, String s2) {
        return LevenshteinDistance.compute(s1, s2); // 返回编辑距离
    }

    public static double jaccardSimilarity(String s1, String s2) {
        // 实现代码...
        return JaccardSimilarity.compute(s1, s2); // 返回Jaccard相似系数
    }

    public static double cosineSimilarity(String s1, String s2) {
        // 实现代码...
        return CosineSimilarity1.compute(s1, s2); // 返回余弦相似性
    }

    public static double computeCombinedSimilarity(String s1, String s2, Map<String, Double> weights) {
        // 计算各个指标
        int levDist = levenshteinDistance(s1, s2);
        double jaccSim = jaccardSimilarity(s1, s2);
        double cosSim = cosineSimilarity(s1, s2);

        // 将编辑距离转换为相似度（假设最大可能距离为s1和s2长度之和）
        double maxDist = s1.length() + s2.length();
        double levSim = 1 - (double) levDist / maxDist;

        System.out.println("levSim距离相似度:" + levSim);
        System.out.println("jaccSim集合相似度:" + jaccSim);
        System.out.println("cosSim余弦相似度:" + cosSim);
        // 根据权重进行加权平均
        double combinedScore = 0.0;
        for (Map.Entry<String, Double> entry : weights.entrySet()) {
            switch (entry.getKey()) {
                case "levenshtein":
                    combinedScore += levSim * entry.getValue();
                    break;
                case "jaccard":
                    combinedScore += jaccSim * entry.getValue();
                    break;
                case "cosine":
                    combinedScore += cosSim * entry.getValue();
                    break;
                default:
                    throw new IllegalArgumentException("Unknown similarity metric: " + entry.getKey());
            }
        }

        return combinedScore;
    }

    public static void main(String[] args) {
        Map<String, Double> weights = new HashMap<>();
        weights.put("levenshtein", 0.3);  // 权重分配
        weights.put("jaccard", 0.4);
        weights.put("cosine", 0.3);

//        String text1 = "the quick brown fox";
//        String text2 = "the lazy dog jumps over the quick brown fox";
        String text1 = "System Err";
        String text2 = "internal system error";
        System.out.println("字符：" + text1);
        System.out.println("字符：" + text2);
        double score = computeCombinedSimilarity(text1.toLowerCase(), text2.toLowerCase(), weights);
        System.out.println("Combined Similarity Score: " + score);
    }
}